//
//  ViewController.m
//  环信聊天
//
//  Created by Mac on 16/3/21.
//  Copyright © 2016年 HY. All rights reserved.
//
#define USERINFO @"USERINFO"

#import "ViewController.h"
#import "HUChatVC.h"

@interface ViewController ()

@property (nonatomic, strong) UITextField* userNameText ;
@property (nonatomic, strong) UITextField* passWordText ;
@property (nonatomic, strong) UIButton* loginButton ;
@property (nonatomic, strong) UIButton* registButton ;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSMutableArray* array = [UserDataStoreModel readArrayWithFileName:USERINFO];
    _userNameText.text = array[0];
    _passWordText.text = array[1];
}
- (void)initUI {
    [self.view addSubview:self.userNameText];
    [self.view addSubview:self.passWordText];
    [self.view addSubview:self.loginButton];
    [self.view addSubview:self.registButton];
    
}
- (void)loginAccount {
    [HUAlert showHubWithTitle:@"正在登录" Detaile:nil];
    EMError *error = [[EMClient sharedClient] loginWithUsername:_userNameText.text password:_passWordText.text];
    if (!error) {
        [HUAlert setTitle:@"登录成功" detail:nil afterDely:1];
        NSArray* array = @[_userNameText.text,_passWordText.text];
        [UserDataStoreModel saveDataWithFileName:USERINFO withArray:[array mutableCopy]];
//        HUChatVC* chatVC = [[HUChatVC alloc]initWithConversationChatter:@"123" conversationType:EMConversationTypeChat];
        HUChatVC* chatVC = [[HUChatVC alloc]init];
        [self.navigationController pushViewController:chatVC animated:YES];
    }else {
        [HUAlert setTitle:@"登录失败" detail:nil afterDely:1];
        [HUAlert showMessage:error.errorDescription];
    }
}
- (void)registAccount {
    EMError *error = [[EMClient sharedClient] registerWithUsername:_userNameText.text password:_passWordText.text];
    if (error==nil) {
        [HUAlert showMessage:@"注册成功"];
    }else {
        [HUAlert showMessage:error.errorDescription];
    }
}
#pragma mark Set && Get
- (UITextField *)userNameText {
    if (!_userNameText) {
        _userNameText = [[UITextField alloc]init];
        _userNameText.bounds = CGRectMake(0, 0, 200, 30);
        _userNameText.center = CGPointMake(SCREEN.width / 2, 200);
        _userNameText.borderStyle = UITextBorderStyleRoundedRect;
        _userNameText.placeholder = @"请输入用户名";
    }
    return _userNameText;
}
- (UITextField *)passWordText {
    if (!_passWordText) {
        _passWordText = [[UITextField alloc]init];
        _passWordText.bounds = CGRectMake(0, 0, 200, 30);
        _passWordText.center = CGPointMake(SCREEN.width / 2, 250);
        _passWordText.secureTextEntry = YES ;
        _passWordText.clearsOnBeginEditing = YES ;
        _passWordText.clearButtonMode = UITextFieldViewModeWhileEditing;
        _passWordText.borderStyle = UITextBorderStyleRoundedRect;
        _passWordText.placeholder = @"请输入密码";
    }
    return _passWordText;
}
- (UIButton *)loginButton {
    if (!_loginButton) {
        _loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _loginButton.frame = CGRectMake(CGRectGetMinX(_userNameText.frame), 350, 80, 40);
        [_loginButton setBackgroundColor:BlueColor];
        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
        _loginButton.layer.cornerRadius = 5;
        _loginButton.clipsToBounds = YES ;
        [_loginButton addTarget:self action:@selector(loginAccount) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginButton;
}
- (UIButton *)registButton {
    if (!_registButton) {
        _registButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _registButton.frame = CGRectMake(CGRectGetMaxX(_userNameText.frame) - 80, 350, 80, 40);
        [_registButton setBackgroundColor:BlueColor];
        [_registButton setTitle:@"注册" forState:UIControlStateNormal];
        _registButton.layer.cornerRadius = 5;
        _registButton.clipsToBounds = YES;
        [_registButton addTarget:self action:@selector(registAccount) forControlEvents:UIControlEventTouchUpInside];
    }
    return _registButton;
}
@end
